package com.xxgc.demo.utils;

import com.iflytek.cloud.speech.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

//tts文本转音频Pcm格式
public class SpeechSynthesisUtil {

    public static void main(String[] args) {
        SpeechSynthesisUtil ttsTool = new SpeechSynthesisUtil("0adfa098");
        ttsTool.textToVoice("马永贞你的妈妈拿了两瓶旺仔牛奶，在校门口等你");
    }


    private static Logger LOGGER = LoggerFactory.getLogger(SpeechSynthesisUtil.class);

    private Object lock = new Object();

    // 语音合成对象
    private SpeechSynthesizer mTts;



    public SpeechSynthesisUtil(String appId) {
        LOGGER.info("------Speech Utility init tts------");
        SpeechUtility.createUtility(SpeechConstant.APPID + "=" + appId);
        // 初始化合成对象
        mTts = SpeechSynthesizer.createSynthesizer();
        if (mTts != null) {
            // 设置发音人
            mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
            mTts.setParameter(SpeechConstant.SPEED, "120");//设置语速
            mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量，范围0~100
        } else {
            LOGGER.error("tts handler init fail");
        }
    }

    public String textToVoice(String text) {
        String mText = text.trim();
        mTts.startSpeaking( mText, mSynListener );
        return null;
    }

    /**
     * 合成监听器
     */
    private SynthesizerListener mSynListener = new SynthesizerListener() {

        @Override
        public void onSpeakBegin() {
        }

        @Override
        public void onBufferProgress(int progress, int beginPos, int endPos,
                                     String info) {

        }

        @Override
        public void onSpeakPaused() {

        }

        @Override
        public void onSpeakResumed() {

        }

        @Override
        public void onSpeakProgress(int progress, int beginPos, int endPos) {

        }

        @Override
        public void onCompleted(SpeechError error) {

        }


        @Override
        public void onEvent(int eventType, int arg1, int arg2, int arg3, Object obj1, Object obj2) {
            if( SpeechEvent.EVENT_TTS_BUFFER == eventType ){
                System.out.println( "onEvent: type="+eventType
                        +", arg1="+arg1
                        +", arg2="+arg2
                        +", arg3="+arg3
                        +", obj2="+(String)obj2 );
                ArrayList<?> bufs = null;
                if( obj1 instanceof ArrayList<?> ){
                    bufs = (ArrayList<?>) obj1;
                }else{
                    System.out.println( "onEvent error obj1 is not ArrayList !" );
                }//end of if-else instance of ArrayList

                if( null != bufs ){
                    for( final Object obj : bufs ){
                        if( obj instanceof byte[] ){
                            final byte[] buf = (byte[]) obj;
                            System.out.println( "onEvent buf length: "+buf.length );
                        }else{
                            System.out.println( "onEvent error element is not byte[] !" );
                        }
                    }//end of for
                }//end of if bufs not null
            }//end of if tts buffer event
            //以下代码用于调试，如果出现问题可以将sid提供给讯飞开发者，用于问题定位排查
			/*else if(SpeechEvent.EVENT_SESSION_ID == eventType) {
				DebugLog.Log("sid=="+(String)obj2);
			}*/
        }
    };
}